#include <stdio.h>
#include <string.h>
#if defined(TEST_TARGET_xml)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/xml.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "xml.h"
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

// #define EXIT_TEST
extern uint64_t unitt_clock(void);

static int test_0(void)
{
    for (int i = 0; i < 100; i++)
    {
        if (0) 
        {
            
            #if defined (EXIT_TEST)
            exit(0);
            #endif 
            return UNITT_E_FAIL;
        }
    }
    
    return UNITT_E_OK;
}

static void unitt_task(void)
{
    static UNITT_TCASE rand_tests[] = {
        UNITT_TCASE(test_0),
        // UNITT_TCASE(test_1),
        // UNITT_TCASE(test_2),
    };

    static UNITT suites[] = {
        { "xxx suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
    };

    UNITT_EXE(suites);
}

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/


#define READ_FILE "test/file/read.xml"
#define WRITE_FILE "test/file/write.xml"

static void xml_preview(xml_t xml)
{
    char *text = NULL;
    text = xml_dumps(xml, 0, 0, NULL);
    if (text)
    {
        printf("%s\r\n", text);
        free(text);
    }
    else  
    {
        printf("[ERROR] dumps fail!!!\r\n");
    }
}

static void test_create(void)
{
    xml_t xml = NULL;
    xml = xml_create("root");
    if (xml)
    {
        printf("xml_create success!!! %p\r\n", xml);
    }
    xml_delete(xml);
}

static void test_set(void)
{
    xml_t xml = NULL;

    /* create root node */
    xml = xml_create("root");

    xml_set_text(xml, "This is xml!!!");

    xml_preview(xml);

    printf("xml_get_text %s\r\n", xml_get_text(xml));

    xml_delete(xml);
}

static void test_attr(void)
{
    xml_t xml = NULL;

    /* create root node */
    xml = xml_create("root");

    xml_add_attribute(xml, "Author", "Lamdonn");
    xml_add_attribute(xml, "Email", "Lamdonn@163.com");
    xml_add_attribute(xml, "License", "GPL-2");

    xml_preview(xml);

    xml_remove_attribute(xml, "Email", 0);

    xml_preview(xml);
    
    printf("attr %s\r\n", xml_get_attribute(xml, "Author", 0));

    xml_delete(xml);
}

static void test_insert(void)
{
    xml_t xml = NULL;
    xml_t node = NULL;

    /* create root node */
    xml = xml_create("root");

    node = xml_create("Author");
    xml_set_text(node, "Lamdonn");
    xml_insert(xml, 0, node);

    node = xml_create("Email");
    xml_set_text(node, "Lamdonn@163.com");
    xml_insert(xml, 1, node);

    node = xml_create("License");
    xml_set_text(node, "GPL-2");
    xml_insert(xml, 2, node);

    xml_preview(xml);

    xml_remove(xml, "Email", 0);

    xml_delete(xml);
}

static void test_to(void)
{
    xml_t xml = NULL;
    xml_t node = NULL;

    /* create root node */
    xml = xml_create("root");

    node = xml_create("node");
    xml_set_text(node, "Chinese");
    xml_insert(xml, 0, node);

    node = xml_create("node");
    xml_set_text(node, "English");
    xml_insert(xml, 1, node);

    node = xml_create("node");
    xml_set_text(node, "Franch");
    xml_insert(xml, 2, node);

    xml_preview(xml);

    node = xml_to(xml, "node", 0);
    printf("xml_get_text %s\r\n", xml_get_text(node));

    node = xml_to(xml, "node", 2);
    printf("xml_get_text %s\r\n", xml_get_text(node));

    xml_delete(xml);
}

static void test_load(void)
{
    xml_t root, x;
    
    root = xml_file_load(READ_FILE);
    if (!root) return;

    printf("load success!\r\n");

    x = xml_to(root, "book", 1);
    printf("x attr: %s\r\n", xml_get_attribute(x, NULL, 0));

    x = xml_to(x, "author", 0);
    printf("author: %s\r\n", xml_get_text(x));

    xml_delete(root);
}

static void test_dump(void)
{
    xml_t root, x;

    root = xml_create("root");
    if (!root) return;

    x = xml_create("name");
    xml_set_text(x, "xml parser");
    xml_insert(root, 0, x);

    x = xml_create("description");
    xml_set_text(x, "This is a C language version of xml parser.");
    xml_insert(root, 1, x);

    x = xml_create("license"); 
    xml_set_text(x, "GPL3.0");
    xml_insert(root, 2, x);

    char *s = xml_dumps(root, 1, 0, NULL);
    if (s)
    {
        printf("%s", s);
        free(s);
    }
    
    xml_file_dump(root, WRITE_FILE);

    xml_delete(root);
}

static void test_base(void)
{
    // test_dump();
    test_load();
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: xml [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"                        <ut>        Unit test\n"
"                        <create>    Test create and delete functions\n"
"                        <set>       Test set and get value functions\n"
"                        <attr>      Test set, get and remove attrubutes functions\n"
"                        <insert>    Test insert and remove node functions\n"
"                        <to>        Test get child node functions\n"
"                        <dump>      Test dump functions\n"
"                        <load>      Test load functions\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"    -u [<period>]       Unit test period, unit ms, the default is 1000ms\n"
"\n"

    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    int ut_period = 1000;

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvu::");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'u' :
            if (command_optarg) ut_period = atoi(command_optarg);
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("xml version %d.%d.%d\r\n", XML_V_MAJOR, XML_V_MINOR, XML_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "ut"))
        {
            srand((unsigned int)time(NULL));
            #if defined(TEST_TARGET_json)
            while (1)
            {
                unitt_task();
                usleep(1000 * ut_period);
            }
            #else  
            printf("create task %d\r\n", task_create(ut_period, unitt_task));
            #endif
        }
        else if (!strcmp(execute, "create"))
        {
            test_create();
        }
        else if (!strcmp(execute, "set"))
        {
            test_set();
        }
        else if (!strcmp(execute, "attr"))
        {
            test_attr();
        }
        else if (!strcmp(execute, "insert"))
        {
            test_insert();
        }
        else if (!strcmp(execute, "to"))
        {
            test_to();
        }
        else if (!strcmp(execute, "dump"))
        {
            test_dump();
        }
        else if (!strcmp(execute, "load"))
        {
            test_load();
        }
    }
    else  
    {
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_xml)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_xml(void)
{
    command_export("xml", test);
}
init_export_app(test_xml);
#endif 
